

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>kospeech.models.transformer.mask &mdash; KoSpeech 0.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    
    <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../../index.html" class="icon icon-home"> KoSpeech
          

          
          </a>

          
            
            
              <div class="version">
                0.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">NOTES</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../notes/intro.html">Intro</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../notes/Preparation.html">Preparation before Training</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../notes/opts.html">Options</a></li>
</ul>
<p class="caption"><span class="caption-text">ARCHITECTURE</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../Seq2seq.html">Seq2seq</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../Transformer.html">Transformer</a></li>
</ul>
<p class="caption"><span class="caption-text">PACKAGE REFERENCE</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../Checkpoint.html">Checkpoint</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../Data.html">Data</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../Decode.html">Decode</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../Evaluator.html">Evaluator</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../Optim.html">Optim</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../Trainer.html">Trainer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../Etc.html">Etc</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../index.html">KoSpeech</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../../index.html">Module code</a> &raquo;</li>
        
      <li>kospeech.models.transformer.mask</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for kospeech.models.transformer.mask</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">from</span> <span class="nn">torch</span> <span class="k">import</span> <span class="n">Tensor</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="k">import</span> <span class="n">Any</span><span class="p">,</span> <span class="n">Optional</span>


<div class="viewcode-block" id="get_pad_mask"><a class="viewcode-back" href="../../../../Transformer.html#kospeech.models.transformer.mask.get_pad_mask">[docs]</a><span class="k">def</span> <span class="nf">get_pad_mask</span><span class="p">(</span><span class="n">inputs</span><span class="p">:</span> <span class="n">Tensor</span><span class="p">,</span> <span class="n">input_lengths</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">Any</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="n">pad_id</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="kc">None</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Tensor</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Padding position is set to True, either use input_lengths or pad_id</span>

<span class="sd">    Examples::</span>
<span class="sd">        &gt;&gt;&gt; get_pad_mask(inputs, input_lengths)</span>
<span class="sd">        [[0, 0, 0, 0, 0, 0, 1, 1, 1, 1],</span>
<span class="sd">         [0, 0, 0, 0, 0, 1, 1, 1, 1, 1],</span>
<span class="sd">         [0, 0, 0, 1, 1, 1, 1, 1, 1, 1],</span>
<span class="sd">         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],</span>
<span class="sd">         [0, 0, 0, 0, 1, 1, 1, 1, 1, 1],</span>
<span class="sd">         [0, 0, 0, 0, 0, 0, 0, 1, 1, 1],</span>
<span class="sd">         [0, 0, 0, 0, 0, 0, 0, 1, 1, 1],</span>
<span class="sd">         [0, 0, 0, 0, 0, 0, 0, 0, 0, 1],</span>
<span class="sd">         [0, 0, 0, 0, 0, 1, 1, 1, 1, 1],</span>
<span class="sd">         [0, 0, 0, 0, 0, 0, 1, 1, 1, 1]] x batch_size</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">assert</span> <span class="p">(</span><span class="n">input_lengths</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">pad_id</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">)</span> <span class="ow">or</span> <span class="p">(</span><span class="n">input_lengths</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">pad_id</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">input_lengths</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">batch_size</span> <span class="o">=</span> <span class="n">inputs</span><span class="o">.</span><span class="n">size</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
        <span class="n">pad_mask</span> <span class="o">=</span> <span class="n">inputs</span><span class="o">.</span><span class="n">new_zeros</span><span class="p">(</span><span class="n">inputs</span><span class="o">.</span><span class="n">size</span><span class="p">()[:</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>  <span class="c1"># N x T</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">batch_size</span><span class="p">):</span>
            <span class="n">pad_mask</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">input_lengths</span><span class="p">[</span><span class="n">i</span><span class="p">]:]</span> <span class="o">=</span> <span class="mi">1</span>

    <span class="k">else</span><span class="p">:</span>
        <span class="k">assert</span> <span class="n">inputs</span><span class="o">.</span><span class="n">dim</span><span class="p">()</span> <span class="o">==</span> <span class="mi">2</span>
        <span class="n">pad_mask</span> <span class="o">=</span> <span class="n">inputs</span><span class="o">.</span><span class="n">eq</span><span class="p">(</span><span class="n">pad_id</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">pad_mask</span><span class="o">.</span><span class="n">unsqueeze</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">bool</span><span class="p">()</span></div>


<div class="viewcode-block" id="get_subsequent_mask"><a class="viewcode-back" href="../../../../Transformer.html#kospeech.models.transformer.mask.get_subsequent_mask">[docs]</a><span class="k">def</span> <span class="nf">get_subsequent_mask</span><span class="p">(</span><span class="n">inputs</span><span class="p">:</span> <span class="n">Tensor</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Tensor</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Makes subsequent masking like following:</span>

<span class="sd">    Examples::</span>
<span class="sd">        &gt;&gt;&gt; get_subsequent_mask(inputs)</span>
<span class="sd">        [[0, 1, 1, 1, 1, 1, 1, 1, 1, 1],</span>
<span class="sd">         [0, 0, 1, 1, 1, 1, 1, 1, 1, 1],</span>
<span class="sd">         [0, 0, 0, 1, 1, 1, 1, 1, 1, 1],</span>
<span class="sd">         [0, 0, 0, 0, 1, 1, 1, 1, 1, 1],</span>
<span class="sd">         [0, 0, 0, 0, 0, 1, 1, 1, 1, 1],</span>
<span class="sd">         [0, 0, 0, 0, 0, 0, 1, 1, 1, 1],</span>
<span class="sd">         [0, 0, 0, 0, 0, 0, 0, 1, 1, 1],</span>
<span class="sd">         [0, 0, 0, 0, 0, 0, 0, 0, 1, 1],</span>
<span class="sd">         [0, 0, 0, 0, 0, 0, 0, 0, 0, 1],</span>
<span class="sd">         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] x batch_size</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">batch_size</span><span class="p">,</span> <span class="n">seq_length</span> <span class="o">=</span> <span class="n">inputs</span><span class="o">.</span><span class="n">size</span><span class="p">()</span>
    <span class="n">subsequent_mask</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">triu</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">ones</span><span class="p">((</span><span class="n">seq_length</span><span class="p">,</span> <span class="n">seq_length</span><span class="p">),</span> <span class="n">device</span><span class="o">=</span><span class="n">inputs</span><span class="o">.</span><span class="n">device</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">torch</span><span class="o">.</span><span class="n">uint8</span><span class="p">),</span> <span class="n">diagonal</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
    <span class="n">subsequent_mask</span> <span class="o">=</span> <span class="n">subsequent_mask</span><span class="o">.</span><span class="n">unsqueeze</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span><span class="o">.</span><span class="n">expand</span><span class="p">(</span><span class="n">batch_size</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>  <span class="c1"># BxTxT</span>

    <span class="k">return</span> <span class="n">subsequent_mask</span><span class="o">.</span><span class="n">bool</span><span class="p">()</span></div>


<div class="viewcode-block" id="get_attn_pad_mask"><a class="viewcode-back" href="../../../../Transformer.html#kospeech.models.transformer.mask.get_attn_pad_mask">[docs]</a><span class="k">def</span> <span class="nf">get_attn_pad_mask</span><span class="p">(</span><span class="n">key</span><span class="p">:</span> <span class="n">Tensor</span><span class="p">,</span> <span class="n">pad_id</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Tensor</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot; For masking out the padding part of key sequence. &quot;&quot;&quot;</span>
    <span class="c1"># Expand to fit the shape of key query attention matrix.</span>
    <span class="n">attn_pad_mask</span> <span class="o">=</span> <span class="n">key</span><span class="o">.</span><span class="n">eq</span><span class="p">(</span><span class="n">pad_id</span><span class="p">)</span>
    <span class="n">attn_pad_mask</span> <span class="o">=</span> <span class="n">attn_pad_mask</span><span class="o">.</span><span class="n">unsqueeze</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">expand</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="n">key</span><span class="o">.</span><span class="n">size</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>  <span class="c1"># b x lq x lk</span>

    <span class="k">return</span> <span class="n">attn_pad_mask</span><span class="o">.</span><span class="n">bool</span><span class="p">()</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2020, Soohwan Kim

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>